﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using NHibernate;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Controllers
{
    public class TeamController : Controller
    {
        public UsersModel ObjUsers;
        public TeamModel ObjTeam;
        public TeamMemberModel ObjTeamMember;
        private ISession _session = NHibernateHelper.GetSession();

        public TeamController()
        {
            ObjUsers = new UsersModel();
            ObjTeam = new TeamModel();
            ObjTeamMember = new TeamMemberModel();
        }

        // 
        // GET: /Team/ 
        [HttpGet]
        public ActionResult Index()
        {
            IList<Team> teamList = ObjTeam.GetTeams();
            return View(teamList);
        }

        // 
        // GET: /Team/Details/
        [HttpGet]
        public ActionResult Details(Guid id)
        {
            Team team = ObjTeam.GetTeamById(id);
            return View(team);
        }

        // 
        // GET: /Team/Create 
        [HttpGet]
        public ActionResult New()
        {
            var team = new Team();
            return View(team);
        }

        // 
        // POST: /Team/Create 
        [HttpPost]
        public ActionResult New(Team team)
        {
            try
            {
                ObjTeam.NewTeam(team);
                var teamMember = new TeamMember
                    {
                        Team = team, Users = ObjUsers.GetUsersById(Client.Instance.ClientId)
                    };
                ObjTeamMember.NewTeamMember(teamMember);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /Team/Edit/5 
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            Team team = ObjTeam.GetTeamById(id);
            return View(team);
        }

        // 
        // POST: /Team/Edit/5 
        [HttpPost]
        public ActionResult Edit(Guid id, Team team)
        {
            try
            {
                ObjTeam.UpdateTeam(team);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /Team/Delete/5 
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            Team team = ObjTeam.GetTeamById(id);
            return View(team);
        }

        // 
        // POST: /Team/Delete/5 
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                Team team = ObjTeam.GetTeamById(id);
                ObjTeam.DeleteTeam(team);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Team/AddMember
        public ActionResult AddMember(Team team)
        {
            var teamMember = new TeamMember
            {
                Team = team
            };
            return View(teamMember);
        }

        //
        // POST: /Team/AddMember
        [HttpPost]
        public ActionResult AddMember(Guid id, TeamMember teamMember)
        {
            try
            {
                teamMember.Users = ObjUsers.GetUsersById(Client.Instance.ClientId);
                ObjTeamMember.NewTeamMember(teamMember);
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                return View();
            }
        }
    }
}